package com.testingedu.jdbcDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.testingedu.common.QueryResult;
import com.testingedu.model.TRole;
import com.testingedu.utils.Format;

public class TRoleDao extends BaseDao<TRole>{
	
	public TRoleDao() {
		clazz = TRole.class;
	}
	
	public void list(QueryResult queryResult, int userId) throws Exception{
		String sql = "select * from t_role tr where tr.id in (select triu.role_id from t_role_in_user triu where triu.user_id = ?)";
		List<Object> values = new ArrayList<Object>();
		values.add(userId);
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.toString(), values);
			queryResult.setResultList(_resultSetToList(rs));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			close();
		}
	}
	
	public void list(QueryResult queryResult, TRole tRole) throws Exception{
		StringBuffer sql = new StringBuffer("select * from t_role tp where tp.user_id = ?");
		List<Object> values = new ArrayList<Object>();
		values.add(tRole.getTUser().getId());
		if(tRole != null) {
			if(tRole.getName() != null && tRole.getName().trim().length() > 0) {
				sql.append(" and tp.name like ?");
				values.add("%"+tRole.getName()+"%");
			}
		}
		sql.append(" limit ?,?");
		List<Object> countValues = new ArrayList<Object>(values);
		values.add(queryResult.getStartRow());
		values.add(queryResult.getPageSize());
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.toString(), values);
			queryResult.setResultList(_resultSetToList(rs));
		}
		finally {
			close();
		}
		sql.replace(7, 8, "count(*)");
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.substring(0, sql.length() - 10), countValues);
			rs.next();
			queryResult.setAllCount(new Long(rs.getInt(1)));
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
	}
	
	public int add(TRole tRole) throws Exception{
		String sql = "insert into t_role(name,add_time,description,user_id) values(?,?,?,?)";
		List<Object> values = new ArrayList<Object>();
		values.add(tRole.getName());
		values.add(Format.formatDateSecond(new Date()));
		values.add(tRole.getDescription());
		values.add(tRole.getTUser().getId());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
		sql = "select max(id) as _id from t_role";
		int id = 0;
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql, null);
			if (rs.next()) {
				id = rs.getInt("_id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
		return id;
	}
	
	public void update(TRole tRole) throws Exception{
		String sql = "update t_role set name = ?, description = ? where id=?";
		List<Object> values = new ArrayList<Object>();
		values.add(tRole.getName());
		values.add(tRole.getDescription());
		values.add(tRole.getId());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
	}
	
	public void delete(int id) throws Exception{
		String sql = "delete from t_role where id = ?";
		try {
			this.myJdbcSetValuesAndExecuteDelete(sql, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
	}
	
	public TRole info(int id) throws Exception{
		String sql = "select * from t_role tp where tp.id = ?";
		List<TRole> result = null;
		try {
			this.myJdbcSetValuesAndExecuteLoad(sql, id);
			result = _resultSetToList(rs);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
		if(result != null && result.size() > 0) {
			return result.get(0);
		}
		return null;
	}
	
	public List<TRole> _resultSetToList(ResultSet rs) throws Exception{
		List<TRole> result = new ArrayList<TRole>();
		try {
			while(rs.next()) {
				TRole tRole = new TRole();
				tRole.setId(rs.getInt("id"));
				tRole.setAddTime(rs.getString("add_time"));
				tRole.setAddTime(tRole.getAddTime().substring(0, tRole.getAddTime().length() - 2));
				tRole.setName(rs.getString("name"));
				tRole.setDescription(rs.getString("description"));
				result.add(tRole);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		}
		return result;
	}
}